<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="商户" prop="shopId">
        <el-select v-model="queryParams.shopId" placeholder="请选择所属商家" clearable filterable
                   size="small" >
          <el-option  v-for="shop in shopList"
                      :key="shop.id"
                      :label="shop.name"
                      :value="shop.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="混剪状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="混剪状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addMerge"
          v-hasPermi="['resource:resource:add']"
        >合成
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="mixshearlist">
      <el-table-column label="序号" prop="id"/>
      <el-table-column label="商户名称" prop="shopName"/>
      <el-table-column label="混剪结果" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0" style="color: green">成功</span>
          <span v-if="scope.row.status == 1" style="color: red">失败</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" prop="createTime"/>
      <el-table-column label="结果视频" prop="resourceUrl"  :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="playVideo(scope.row)"
            ref="btn"
            size="mini"
          >
            播 放
          </el-button>
          <!-- <a v-for="url in scope.row.resourceUrlArry" style="color: #00afff;display: block;" :href="url"
             target="view_window">{{url}}</a> -->
        </template>
      </el-table-column>
      <el-table-column label="bgm" prop="bgmName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.shopId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title
      :visible.sync="dialogPlay"
      width="30%"
      @close="closeDialog()"
    >
      <video
        :src="url"
        controls
        autoplay
        class="video"
        width="100%"
      ></video>
    </el-dialog>

    <!-- 合成对话框 -->
    <el-dialog
      title="添加混剪任务"
      :visible.sync="openAddMixshearDialog"
      width="600px"
      :show-close="false"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="商户">
          <el-select
            v-model="form.shopId"
            filterable   @change="selectShop"
          >
            <el-option
              v-for="item in shopList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公共Bgm" prop="global">
          <el-select  v-model="form.global">
            <el-option label="使用"  value="0">
            </el-option>
            <el-option label="不使用" value="1" >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合成数量" prop="quantity">
          <el-input type="text" v-model="form.quantity" />
          <span style="color: orangered">最多可合成{{maxMergeCount}}个视频</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mixshearList,delmixshear,addMixshearTask} from "@/api/mixshear/mixshear";
import {listAllShop} from "@/api/merchant/merchant";
import {resourceAdd, resourceEdit,getMaxMergeVideoCount} from "@/api/resource/resource";
export default {
  data(){
    return{
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 混剪列表格数据
      mixshearlist: [ ],
      //混剪数据总条数
      total: 0,
      // 状态数据字典
      statusOptions: [
        {dictLabel: '成功', dictValue: '0'}, {dictLabel: '失败', dictValue: '1'}
      ],
      shopList:[],
      //视频弹窗
      dialogPlay: false,
      //视频地址
      url: "",
      openAddMixshearDialog:false,
      form:{},
      maxMergeCount:0
    }
  },
  created() {
    this.getList();
    this.getAllShop();
  },
  methods: {
    /*查询最大可合成数量*/
    getMaxMergeCount(shopId){
      this.loading = true;
      getMaxMergeVideoCount(shopId).then(
        (response) => {
          this.maxMergeCount = response.data;
          this.loading = false;
        }
      );
    },
    /**查询所有商户列表*/
    getAllShop(){
      this.loading = true;
      listAllShop().then(
        (response) => {
          this.shopList = response.data;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询混剪视频列表 */
    getList() {
      this.loading = true;
      mixshearList(this.queryParams).then((res) => {
        this.mixshearlist = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mixIds = row.id || this.ids;
      this.$confirm(
        '是否确认删除混剪视频编号为"' + mixIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delmixshear(mixIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 视频播放
    playVideo(row) {
      this.dialogPlay = true;
      this.url = row.playUrl;
    },
    closeDialog() {
      this.url = ""; //清空数据 关闭视频播放
    },
    // 取消按钮
    cancel() {
      this.openAddMixshearDialog = false;
      this.form.shopId = "";
      this.form.quantity = "";
    },
    //添加合成任务
    addMerge(){
      this.openAddMixshearDialog=true;
    },
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addMixshearTask(this.form).then(response => {
            this.msgSuccess('添加合成任务成功');
            this.openAddMixshearDialog = false
            this.getList();
          });
        }
      });
    },
    //选择商家
    selectShop(value){
      this.getMaxMergeCount(value);
    }
  }

}
</script>

<style>

</style>
